home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT1-9.ZIP / TUTPROG2.PAS < prev    next >
Pascal/Delphi Source File  |  1993-07-07  |  10KB  |  330 lines

  1. {$X+}
  2.  
  3. Uses Crt;
  4.  
  5. CONST VGA=$a000;
  6.  
  7. Var Pall,Pall2 : Array[0..255,1..3] of Byte;
  8.      { This declares the PALL variable. 0 to 255 signify the colors of the
  9.        pallette, 1 to 3 signifies the Red, Green and Blue values. I am
  10.        going to use this as a sort of "virtual pallette", and alter it
  11.        as much as I want, then suddenly bang it to screen. Pall2 is used
  12.        to "remember" the origional pallette so that we can restore it at
  13.        the end of the program. }
  14.  
  15.  
  16.  
  17. {──────────────────────────────────────────────────────────────────────────}
  18. Procedure SetMCGA;  { This procedure gets you into 320x200x256 mode. }
  19. BEGIN
  20.   asm
  21.      mov        ax,0013h
  22.      int        10h
  23.   end;
  24. END;
  25.  
  26.  
  27. {──────────────────────────────────────────────────────────────────────────}
  28. Procedure SetText;  { This procedure returns you to text mode.  }
  29. BEGIN
  30.   asm
  31.      mov        ax,0003h
  32.      int        10h
  33.   end;
  34. END;
  35.  
  36.  
  37. {──────────────────────────────────────────────────────────────────────────}
  38. procedure WaitRetrace; assembler;
  39.   { This waits until you are in a Verticle Retrace ... this means that all
  40.     screen manipulation you do only appears on screen in the next verticle
  41.     retrace ... this removes most of the "fuzz" that you see on the screen
  42.     when changing the pallette. It unfortunately slows down your program
  43.     by "synching" your program with your monitor card ... it does mean
  44.     that the program will run at almost the same speed on different
  45.     speeds of computers which have similar monitors. In our SilkyDemo,
  46.     we used a WaitRetrace, and it therefore runs at the same (fairly
  47.     fast) speed when Turbo is on or off. }
  48.  
  49. label
  50.   l1, l2;
  51. asm
  52.     mov dx,3DAh
  53. l1:
  54.     in al,dx
  55.     and al,08h
  56.     jnz l1
  57. l2:
  58.     in al,dx
  59.     and al,08h
  60.     jz  l2
  61. end;
  62.  
  63.  
  64. {──────────────────────────────────────────────────────────────────────────}
  65. Procedure GetPal(ColorNo : Byte; Var R,G,B : Byte);
  66.   { This reads the values of the Red, Green and Blue values of a certain
  67.     color and returns them to you. }
  68. Begin
  69.    Port[$3c7] := ColorNo;
  70.    R := Port[$3c9];
  71.    G := Port[$3c9];
  72.    B := Port[$3c9];
  73. End;
  74.  
  75.  
  76. {──────────────────────────────────────────────────────────────────────────}
  77. Procedure Pal(ColorNo : Byte; R,G,B : Byte);
  78.   { This sets the Red, Green and Blue values of a certain color }
  79. Begin
  80.    Port[$3c8] := ColorNo;
  81.    Port[$3c9] := R;
  82.    Port[$3c9] := G;
  83.    Port[$3c9] := B;
  84. End;
  85.  
  86.  
  87. {──────────────────────────────────────────────────────────────────────────}
  88. Procedure Putpixel (X,Y : Integer; Col : Byte);
  89.   { This puts a pixel on the screen by writing directly to memory. }
  90. BEGIN
  91.   Mem [VGA:X+(Y*320)]:=Col;
  92. END;
  93.  
  94.  
  95. {──────────────────────────────────────────────────────────────────────────}
  96. Procedure line(a,b,c,d,col:integer);
  97.   { This draws a line from a,b to c,d of color col. }
  98.    Function sgn(a:real):integer;
  99.    BEGIN
  100.         if a>0 then sgn:=+1;
  101.         if a<0 then sgn:=-1;
  102.         if a=0 then sgn:=0;
  103.    END;
  104. var u,s,v,d1x,d1y,d2x,d2y,m,n:real;
  105.     i:integer;
  106. BEGIN
  107.      u:= c - a;
  108.      v:= d - b;
  109.      d1x:= SGN(u);
  110.      d1y:= SGN(v);
  111.      d2x:= SGN(u);
  112.      d2y:= 0;
  113.      m:= ABS(u);
  114.      n := ABS(v);
  115.      IF NOT (M>N) then
  116.      BEGIN
  117.           d2x := 0 ;
  118.           d2y := SGN(v);
  119.           m := ABS(v);
  120.           n := ABS(u);
  121.      END;
  122.      s := INT(m / 2);
  123.      FOR i := 0 TO round(m) DO
  124.      BEGIN
  125.           putpixel(a,b,col);
  126.           s := s + n;
  127.           IF not (s<m) THEN
  128.           BEGIN
  129.                s := s - m;
  130.                a:= a +round(d1x);
  131.                b := b + round(d1y);
  132.           END
  133.           ELSE
  134.           BEGIN
  135.                a := a + round(d2x);
  136.                b := b + round(d2y);
  137.           END;
  138.      END;
  139. END;
  140.  
  141.  
  142. {──────────────────────────────────────────────────────────────────────────}
  143. Procedure PalPlay;
  144.   { This procedure mucks about with our "virtual pallette", then shoves it
  145.     to screen. }
  146. Var Tmp : Array[1..3] of Byte;
  147.   { This is used as a "temporary color" in our pallette }
  148.     loop1 : Integer;
  149. BEGIN
  150.    Move(Pall[200],Tmp,3);
  151.      { This copies color 200 from our virtual pallette to the Tmp variable }
  152.    Move(Pall[0],Pall[1],200*3);
  153.      { This moves the entire virtual pallette up one color }
  154.    Move(Tmp,Pall[0],3);
  155.      { This copies the Tmp variable to the bottom of the virtual pallette }
  156.    WaitRetrace;
  157.    For loop1:=1 to 255 do
  158.      pal (loop1,pall[loop1,1],pall[loop1,2],pall[loop1,3]);
  159. END;
  160.  
  161.  
  162. {──────────────────────────────────────────────────────────────────────────}
  163. Procedure SetUpScreen;
  164.   { This gets our screen ready but setting up the pallette and drawing
  165.     the lines. }
  166. Var Loop : Integer;
  167. BEGIN
  168.    FillChar(Pall,SizeOf(Pall),0);
  169.        { Clear the entire PALL variable to zero. }
  170.    For Loop := 0 to 200 do BEGIN
  171.       Pall[Loop,1] := Loop mod 64;
  172.    END;
  173.        { This sets colors 0 to 200 in the PALL variable to values between
  174.          0 to 63. the MOD function gives you the remainder of a division,
  175.          ie. 105 mod 10 = 5 }
  176.  
  177.    For Loop := 1 to 320 do BEGIN
  178.       Line(319,199,320-Loop,0,(Loop Mod 199)+1);
  179.       Line(0,0,Loop,199,(Loop Mod 199)+1);
  180.        { These two lines start drawing lines from the left and the right
  181.          hand sides of the screen, using colors 1 to 199. Look at these
  182.          two lines and understand them. }
  183.       PalPlay;
  184.         { This calls the PalPlay procedure }
  185.    END;
  186. END;
  187.  
  188.  
  189. {──────────────────────────────────────────────────────────────────────────}
  190. Procedure GrabPallette;
  191. VAR loop1:integer;
  192. BEGIN
  193.   For loop1:=0 to 255 do
  194.     Getpal (loop1,pall2[loop1,1],pall2[loop1,2],pall2[loop1,3]);
  195. END;
  196.  
  197.  
  198.  
  199. {──────────────────────────────────────────────────────────────────────────}
  200. Procedure Blackout;
  201.   { This procedure blackens the screen by setting the pallette values of
  202.     all the colors to zero. }
  203. VAR loop1:integer;
  204. BEGIN
  205.   WaitRetrace;
  206.   For loop1:=0 to 255 do
  207.     Pal (loop1,0,0,0);
  208. END;
  209.  
  210.  
  211. {──────────────────────────────────────────────────────────────────────────}
  212. Procedure HiddenScreenSetup;
  213.   { This procedure sets up the screen while it is blacked out, so that the
  214.     user can't see what is happening. }
  215. VAR loop1,loop2:integer;
  216. BEGIN
  217.   For loop1:=0 to 319 do
  218.     For loop2:=0 to 199 do
  219.       PutPixel (loop1,loop2,Random (256));
  220. END;
  221.  
  222.  
  223. {──────────────────────────────────────────────────────────────────────────}
  224. Procedure Fadeup;
  225.   { This procedure slowly fades up the new screen }
  226. VAR loop1,loop2:integer;
  227.     Tmp : Array [1..3] of byte;
  228.       { This is temporary storage for the values of a color }
  229. BEGIN
  230.   For loop1:=1 to 64 do BEGIN
  231.       { A color value for Red, green or blue is 0 to 63, so this loop only
  232.         need be executed a maximum of 64 times }
  233.     WaitRetrace;
  234.     For loop2:=0 to 255 do BEGIN
  235.       Getpal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  236.       If Tmp[1]<Pall2[loop2,1] then inc (Tmp[1]);
  237.       If Tmp[2]<Pall2[loop2,2] then inc (Tmp[2]);
  238.       If Tmp[3]<Pall2[loop2,3] then inc (Tmp[3]);
  239.         { If the Red, Green or Blue values of color loop2 are less then they
  240.           should be, increase them by one. }
  241.       Pal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  242.         { Set the new, altered pallette color. }
  243.     END;
  244.   END;
  245. END;
  246.  
  247.  
  248. {──────────────────────────────────────────────────────────────────────────}
  249. Procedure FadeDown;
  250.   { This procedure fades the screen out to black. }
  251. VAR loop1,loop2:integer;
  252.     Tmp : Array [1..3] of byte;
  253.       { This is temporary storage for the values of a color }
  254. BEGIN
  255.   For loop1:=1 to 64 do BEGIN
  256.     WaitRetrace;
  257.     For loop2:=0 to 255 do BEGIN
  258.       Getpal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  259.       If Tmp[1]>0 then dec (Tmp[1]);
  260.       If Tmp[2]>0 then dec (Tmp[2]);
  261.       If Tmp[3]>0 then dec (Tmp[3]);
  262.         { If the Red, Green or Blue values of color loop2 are not yet zero,
  263.           then, decrease them by one. }
  264.       Pal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  265.         { Set the new, altered pallette color. }
  266.     END;
  267.   END;
  268. END;
  269.  
  270.  
  271. {──────────────────────────────────────────────────────────────────────────}
  272. Procedure RestorePallette;
  273.   { This procedure restores the origional pallette }
  274. VAR loop1:integer;
  275. BEGIN
  276.   WaitRetrace;
  277.   For loop1:=0 to 255 do
  278.     pal (loop1,Pall2[loop1,1],Pall2[loop1,2],Pall2[loop1,3]);
  279. END;
  280.  
  281.  
  282. BEGIN
  283.   ClrScr;
  284.   Writeln ('This program will draw lines of different colors across the');
  285.   Writeln ('screen and change them only by changing their pallette values.');
  286.   Writeln ('The nice thing about using the pallette is that one pallette');
  287.   Writeln ('change changes the same color over the whole screen, without');
  288.   Writeln ('you having to redraw it. Because I am using a WaitRetrace');
  289.   Writeln ('command, turning on and off your turbo during the demonstration');
  290.   Writeln ('should have no effect.');
  291.   Writeln;
  292.   Writeln ('The second part of the demo blacks out the screen using the');
  293.   Writeln ('pallette, fades in the screen, waits for a keypress, then fades');
  294.   Writeln ('it out again. I haven''t put in any delays for the fadein/out,');
  295.   Writeln ('so you will have to put ''em in yourself to get it to the speed you');
  296.   Writeln ('like. Have fun and enjoy! ;-)');
  297.   Writeln; Writeln;
  298.   Writeln ('Hit any key to continue ...');
  299.   Readkey;
  300.   SetMCGA;
  301.   GrabPallette;
  302.   SetUpScreen;
  303.   repeat
  304.      PalPlay;
  305.        { Call the PalPlay procedure repeatedly until a key is pressed. }
  306.   Until Keypressed;
  307.   Readkey;
  308.     { Read in the key pressed otherwise it is left in the keyboard buffer }
  309.   Blackout;
  310.   HiddenScreenSetup;
  311.   FadeUp;
  312.   Readkey;
  313.   FadeDown;
  314.   Readkey;
  315.   RestorePallette;
  316.   SetText;
  317.   Writeln ('All done. This concludes the second sample program in the ASPHYXIA');
  318.   Writeln ('Training series. You may reach DENTHOR under the name of GRANT');
  319.   Writeln ('SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the');
  320.   Writeln ('ASPHYXIA BBS. Get the numbers from Roblist, or write to :');
  321.   Writeln ('             Grant Smith');
  322.   Writeln ('             P.O. Box 270');
  323.   Writeln ('             Kloof');
  324.   Writeln ('             3640');
  325.   Writeln ('I hope to hear from you soon!');
  326.   Writeln; Writeln;
  327.   Write   ('Hit any key to exit ...');
  328.   Readkey;
  329. END.
  330.